package com.hornsun.util;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 编译中... on 2018/6/16.
 */
public class StrUtil {

    public static String toDesc(String str){
        str+=" desc";
        return str;
    };


    /**
     * 获取MAC地址
     */
    public static String getMacAddress(InetAddress ia) throws SocketException {
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        //下面代码是把mac地址拼装成String
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            //mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
        //把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }


    public static String macToString(byte[] source){
        byte[] mac = new byte[6];
        System.arraycopy(source,0,mac,0,6);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            //mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
        //把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }

    //获取毫秒数
    public static long getMilliseconds(Integer day) {
        return day * 24 * 60 * 60 * 1000L;
    }

    //byte数组去空格
    public static byte[] arrayTrim(byte[] bytes){
        int length=0  ;
        for (int i = bytes.length-1; i >=0; i--) {
            if (bytes[i] != 0) {
               length=i+1;
               break;
            }
        }
        byte[] newByte = new byte[length];
        System.arraycopy(bytes,0,newByte,0,length);
        return newByte;
    }

    //int数组补位
    public static int[] toIntLongArray(int[] bytes,int length){
        int[] newByte = new int[length];
        System.arraycopy(bytes,0,newByte,0,bytes.length);
        return newByte;
    }

    //byte数组补位
    public static byte[] toByteLongArray(byte[] bytes,int length){
        byte[] newByte = new byte[length];
        System.arraycopy(bytes,0,newByte,0,bytes.length);
        return newByte;
    }

    //时间格式化
    public static String format(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.format(date);
    }

    public static byte[] toUtf8Array(String str) throws UnsupportedEncodingException {
        String newStr = new String(str.getBytes(),"UTF-8");
        return newStr.getBytes();
    }

    public static int dayToMinute(int days){
        return days*24*60;
    }



    /**
     * 剩余时长  / 分钟
     * @param date 授权时间
     * @param days 有效天数
     * @return
     */
    public static int surplusTime(Date date,int days){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long times = (calendar.getTime().getTime()+days*24*60*60*1000L-now.getTime())/1000/60;
        return  (int)times;

    }


    public static int pointsToNum(int online,int totalPoints){
        return (int)(online*Math.pow(2,8)+totalPoints);
    }








}
